package com.gitee.linmt.advice;

import com.gitee.linmt.entity.Result;
import com.gitee.linmt.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Objects;

/**
 * 异常统一处理.
 *
 * @author <a href="mailto:lin-mt@outlook.com">lin-mt</a>
 */
@RestControllerAdvice
public class ServiceExceptionAdvice {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(ServiceExceptionAdvice.class);
    
    /**
     * 统一处理 com.gitee.linmt.exception.ServiceException.
     *
     * @param e ServiceException
     * @return Result
     */
    @ExceptionHandler(value = ServiceException.class)
    public Result<Object> handleServiceException(final ServiceException e) {
        ServiceExceptionAdvice.LOGGER.error("出现业务异常", e);
        if (Objects.nonNull(e.getCode())) {
            return Result.exceptionMsg(e.getCode(), e.getMsgParam());
        }
        return Result.exception().setMessage(e.getMessage());
    }
}
